package rmi.mainframe;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

//import com.sun.xml.internal.fastinfoset.sax.Properties;

import rmi.chatframe.ChatFrame;
import rmi.client.ClientClass;
import rmi.impl.Impl;


public class MainFrame extends JFrame
{
		private static final long serialVersionUID = 1L;
		JPanel namePan;
        JPanel signPan;
        //public JPanel logPan;
        JPanel connPan;
        JLabel nameEnter;
        JTextField name;
        JButton signIn;
        JButton logOut;
        JButton conct;
        JTextField frnd;
                
        public MainFrame()
        {
            super("iMagic");
            setIconImage(Toolkit.getDefaultToolkit().getImage("ilogo.gif"));
            //setSize(300, 450);
            //setVisible(true);
            addWindowListener (new WindowAdapter(){
            	public void windowClosing(WindowEvent e){
            		System.exit(0);
            	}
            });
                
                setLayout(new BorderLayout());
                namePan = new JPanel(new GridLayout(1,0));
                namePan.setLayout(new FlowLayout(FlowLayout.LEFT));
                nameEnter = new JLabel("Name:  ");
                namePan.add(nameEnter);
                name = new JTextField(15);
                namePan.add(name);
                //signPan = new JPanel();
                signIn = new JButton("SignIn");
                namePan.add(signIn);
                signIn.setEnabled(false);
                //add(signPan, BorderLayout.CENTER);
                add(namePan, BorderLayout.NORTH);
                
                connPan = new JPanel(new GridLayout(1,2));
                connPan.setLayout(new FlowLayout(FlowLayout.LEFT));
                logOut = new JButton("LogOut");
                logOut.setEnabled(false);
                connPan.add(logOut);
                conct = new JButton("Friend");
                conct.setEnabled(false);
                connPan.add(conct);
                frnd = new JTextField("", 10);
                frnd.setEnabled(false);
                connPan.add(frnd);
                add(connPan, BorderLayout.SOUTH);
                pack();
                
        }
        
        public void signInMeth()
        {
        	name.addKeyListener(new KeyListener() {
    			public void keyPressed(KeyEvent ke) { }
                public void keyReleased(KeyEvent ke) {
                    if(!(name.getText()).equals(""))
                        signIn.setEnabled(true);
                    else
                        signIn.setEnabled(false);
                    int key = ke.getKeyCode();
                    if(key == KeyEvent.VK_ENTER)
                        if(signIn.isEnabled()){
                        	createNaming(name.getText());
                        	//conct.setEnabled(true);
                        	//logOut.setEnabled(true);
                        	frnd.setEnabled(true);
                            signIn.setEnabled(false);
                            //signIn.setVisible(false);
                            name.setEnabled(false);
                            frnd.requestFocus();
                            //name.setVisible(false);  
                        }
                }
                public void keyTyped(KeyEvent ke) {	}
        	});
        	
        	signIn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                	createNaming(name.getText());
                	//conct.setEnabled(true);
                	//logOut.setEnabled(true);
                	frnd.setEnabled(true);
                        signIn.setEnabled(false);
                    //signIn.setVisible(false);
                    name.setEnabled(false);
                    frnd.requestFocus();
                    //name.setVisible(false);
                }
            });
        }
        
        private void createNaming(String myName)
        {
        	
        	//System.out.println(myName);
        	myName = myName.toUpperCase();
        	final String s = myName;
        	try{
    			Impl obj = new Impl(new ChatFrame(myName));    		
    			Properties prop = new Properties();    		
    			prop.put("java.naming.factory.initial","com.sun.jndi.cosnaming.CNCtxFactory");    		
    			prop.put("java.naming.provider.url","corbaloc:iiop:localhost:20900/NameService");    		
    			Context ctx = new InitialContext(prop);    			
    			ctx.rebind(myName, obj);
    			System.out.println("Server Ready...");    		
    		}catch (Exception e) {
    			System.out.println("Error:"+e.getMessage());
    		}
    		
    		frnd.addKeyListener(new KeyListener(){
                
                    public void keyPressed(KeyEvent ke) { }
                    public void keyReleased(KeyEvent ke) { 
                        if(!(frnd.getText()).equals(""))
                            conct.setEnabled(true);
                        else
                            conct.setEnabled(false);
                        int key = ke.getKeyCode();
                            if(key == KeyEvent.VK_ENTER){
                                new ClientClass(frnd.getText(), s);
                                frnd.setText("");
                                conct.setEnabled(false);
                            }
                        
                    
                    }
                    public void keyTyped(KeyEvent ke) { }
                });
                
                conct.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent ae) {
                	new ClientClass(frnd.getText(), s);
                	frnd.setText("");
                }
    		});
    		
        }
}